/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package icexam.exam.print;

import java.awt.Color;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.jasperreports.engine.JRElement;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRReport;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.base.JRBaseLine;
import net.sf.jasperreports.engine.base.JRBasePrintText;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.design.JRDesignBand;
import net.sf.jasperreports.engine.design.JRDesignExpression;
import net.sf.jasperreports.engine.design.JRDesignField;
import net.sf.jasperreports.engine.design.JRDesignImage;
import net.sf.jasperreports.engine.design.JRDesignStaticText;
import net.sf.jasperreports.engine.design.JRDesignTextField;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.type.ScaleImageEnum;
import org.apache.commons.beanutils.BasicDynaBean;
import org.apache.commons.beanutils.BasicDynaClass;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.DynaProperty;

/**
 *
 * @author Ethan
 */
public class NewReportProcess extends ReportProcess {

    private final static int textWidth = 80;
    private final static int textHeight = 20;
    private final static int columnHeaderfontSize = 8;
    private final static int fontSize = 7;
    private final static int X = 80;
    private final static int Y = 12;
    private final static int columnHeaderHeight = 12 * 3;
    private final static int columnHeaderFooter = 12 * 3;
    private final static int detailHeight = 62;
    private static String aliasColumn = "column";
    private List<JasperPrint> jasperPrints = new ArrayList<JasperPrint>();

    public void addPreview(int type, String headers[], String title1[], String title2[], List<Object[]> list, List<Object[]> footlist, String arg0, String arg1, String arg2) throws Exception {
        String[] alias = preaseAliasColumnHeaders(headers);

        JasperReport jp = getJasperReport(type, headers, alias, title1, title2, footlist, arg0, arg1, arg2);
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("imageParam", "D:/logo.jpg");
        parameters.put("isShowImage", "true");
        JasperPrint jasperPrint = JasperFillManager.fillReport(jp, parameters,
                new JRBeanCollectionDataSource(getBaseList(alias, phrase(list))));
        jasperPrints.add(jasperPrint);
    }

    public static List<String[]> phrase(List<Object[]> list) {
        List<String[]> temps = new ArrayList<String[]>();
        String[] s = null;
        for (Object[] obj : list) {
            s = new String[obj.length];
            for (int i = 0; i < obj.length; i++) {
                if (obj[i] != null) {
                    s[i] = obj[i].toString();
                } else {
                    s[i] = "";
                }

            }
            temps.add(s);
        }
        return temps;
    }

    private static String[] preaseAliasColumnHeaders(String headers[]) {
        int size = headers.length;
        String[] alias = new String[size];
        for (int i = 0; i < size; i++) {
            alias[i] = aliasColumn + i;
        }
        return alias;
    }
    private static int[] wightArray = {20, 400, 55, 55, 40};

    @SuppressWarnings("deprecation")
    private static JasperReport getJasperReport(int type, String[] headers,
            String alias[], String[] title1, String[] title2, List<Object[]> footlist, String reportName, String reportTime, String score) throws JRException {
        JasperDesign design = new JasperDesign();
        design.setName("statistics");
        design.setPrintOrder(JRReport.PRINT_ORDER_VERTICAL);
        design.setOrientation(JRReport.ORIENTATION_PORTRAIT);

        design.setPageWidth(595);
        design.setPageHeight(842);
        design.setColumnWidth(580);
        design.setColumnSpacing(0);
        design.setLeftMargin(15);
        design.setRightMargin(0);
        design.setTopMargin(20);
        design.setBottomMargin(20);
        design.setWhenNoDataType(JRReport.WHEN_NO_DATA_TYPE_BLANK_PAGE);
        design.setTitleNewPage(false);
        design.setSummaryNewPage(false);

        JRDesignBand columnHeader = new JRDesignBand();
        columnHeader.setHeight(62);
        JRDesignBand detail = new JRDesignBand();
        detail.setHeight(detailHeight);
        JRDesignStaticText staticText = new JRDesignStaticText();
        staticText.setText(reportTime);
        staticText.setTextAlignment(JRBasePrintText.HORIZONTAL_ALIGN_CENTER);
        staticText.setFontSize(15);
        staticText.setHeight(40);
        staticText.setWidth(500);
        staticText.setBold(true);
        staticText.setX(0);
        staticText.setY(0);
//            staticText.setBackcolor(new Color(200, 255, 200));
        staticText.setMode(JRElement.MODE_OPAQUE);
//        staticText.setTextAlignment(JRBasePrintText.HORIZONTAL_ALIGN_LEFT);
        columnHeader.addElement(staticText);


        staticText =  (JRDesignStaticText) staticText.clone();
//        if (headers.length >= 5) {
        staticText.setText(reportName);
        staticText.setFontSize(12);
        staticText.setHeight(textHeight);
        staticText.setWidth(400);
        staticText.setBold(true);
        staticText.setX(0);
        staticText.setY(20);
//            staticText.setBackcolor(new Color(200, 255, 200));
        staticText.setMode(JRElement.MODE_OPAQUE);
        staticText.setTextAlignment(JRBasePrintText.HORIZONTAL_ALIGN_LEFT);
//            staticText.setLeftBorder(JRBaseLine.PEN_THIN);
//            staticText.setTopBorder(JRBaseLine.PEN_THIN);
//            staticText.setRightBorder(JRBaseLine.PEN_THIN);
//            staticText.setBottomBorder(JRBaseLine.PEN_THIN);
        columnHeader.addElement(staticText);
        staticText = (JRDesignStaticText) staticText.clone();
        staticText.setX(400);

        staticText.setText("");
        staticText.setWidth(100);
        columnHeader.addElement(staticText);
        staticText = (JRDesignStaticText) staticText.clone();
        staticText.setX(500);
        staticText.setText(score);
        staticText.setWidth(580-500);
        columnHeader.addElement(staticText);


        for (int i = 0; i < headers.length; i++) {

            // add column headers
            staticText = new JRDesignStaticText();
            staticText.setText(title1[i]);
            staticText.setFontSize(12);
            staticText.setHeight(textHeight);
            staticText.setWidth(wightArray[i]);
            staticText.setTextAlignment(JRBasePrintText.HORIZONTAL_ALIGN_CENTER);

            int x = 0;
            for (int j = 0; j < i; j++) {
                x += wightArray[j];
            }
            staticText.setX(x);
            staticText.setY(40);
            staticText.setBackcolor(new Color(200, 255, 200));
            staticText.setMode(JRElement.MODE_OPAQUE);
            columnHeader.addElement(staticText);

            // define fields
            JRDesignField field = new JRDesignField();
            field.setName(alias[i]);
            field.setValueClass(String.class);
            design.addField(field);

            // add text fields for displaying fields
            JRDesignTextField textField = new JRDesignTextField();
            JRDesignExpression expression = new JRDesignExpression();
            expression.setText("$F{" + alias[i] + "}");
            expression.setValueClass(String.class);
            textField.setExpression(expression);
//            if (i == 1) {
//                textField.setFontSize(12);
//            } else {
                textField.setFontSize(12);
//            }
            textField.setHeight(detailHeight);
//            textField.set

//            textField.setY(textHeight*2);
            textField.setWidth(wightArray[i]);
            if (i == 1) {
                textField.setTextAlignment(JRBasePrintText.HORIZONTAL_ALIGN_LEFT);
            } else {
                textField.setTextAlignment(JRBasePrintText.HORIZONTAL_ALIGN_CENTER);
            }
//            if(i==0&&type==0)
//            {
//                textField.setTextAlignment(JRBasePrintText.HORIZONTAL_ALIGN_RIGHT);
//            }else{

//            }
            textField.setX(x);

//            textField.setBackcolor(new Color(255,255,new Integer(expression1.getText()).intValue()));
            textField.setPdfFontName("MHei-Medium");
            textField.setPdfEmbedded(true);
            textField.setPdfEncoding("UniCNS-UCS2-H");
            detail.addElement(textField);

        }
        design.setColumnHeader(columnHeader);
        design.setDetail(detail);
//        JRDesignBand columnFooter = new JRDesignBand();
//        columnFooter.setHeight(columnHeaderFooter);
//        JRDesignStaticText fstaticText = null;
//        for (int i = 0; i < footlist.size(); i++) {
//            for (int j = 0; j < footlist.get(i).length; j++) {
//                fstaticText = new JRDesignStaticText();
//                fstaticText.setText(footlist.get(i)[j].toString());
//                fstaticText.setFontSize(columnHeaderfontSize);
//                fstaticText.setHeight(textHeight);
//                fstaticText.setWidth(textWidth);
//                fstaticText.setBold(true);
//                fstaticText.setX(0 + j * X);
//                fstaticText.setY(0 + i * Y);
//                fstaticText.setBackcolor(new Color(200, 255, 200));
//                fstaticText.setMode(JRElement.MODE_OPAQUE);
//                if(j==0&&type==0)
//                    fstaticText.setTextAlignment(JRBasePrintText.HORIZONTAL_ALIGN_LEFT);
//                else
//                    fstaticText.setTextAlignment(JRBasePrintText.HORIZONTAL_ALIGN_CENTER);
//                fstaticText.setLeftBorder(JRBaseLine.PEN_THIN);
//                fstaticText.setTopBorder(JRBaseLine.PEN_THIN);
//                fstaticText.setRightBorder(JRBaseLine.PEN_THIN);
//                fstaticText.setBottomBorder(JRBaseLine.PEN_THIN);
//                columnFooter.addElement(fstaticText);
//            }
//        }
//        design.setSummary(columnFooter);
        return JasperCompileManager.compileReport(design);
    }

    /**
     * 利用反射機制，裝拼數�?     *
     * @param headers
     * @param list
     * @return
     * @throws Exception
     */
    private static List<Object> getBaseList(String[] headers,
            List<String[]> list) throws Exception {
        List<Object> result = new ArrayList<Object>();
        int length = headers.length;
        DynaProperty[] dynaProps = new DynaProperty[length];
        for (int i = 0; i < length; i++) {
            dynaProps[i] = new DynaProperty(headers[i], String.class);
        }
        BasicDynaClass dynaClass = new BasicDynaClass("first",
                BasicDynaBean.class, dynaProps);
        for (Object[] obj : list) {
            DynaBean employee = dynaClass.newInstance();
            for (int i = 0; i < length; i++) {
                employee.set(headers[i], obj[i]);
            }
            result.add(employee);
        }
        return result;
    }

    public void showAll() {
        new MyReportViewer(jasperPrints);
    }
}
